package com.myssoteka.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.RootLayoutPanel;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TabLayoutPanel;
import com.google.gwt.user.client.ui.TabPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.myssoteka.shared.FieldVerifier;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class Myssoteka implements EntryPoint {
	final TextBox titleField = new TextBox();
	final TextBox authorField = new TextBox();
	final TextBox publisherField = new TextBox();
	final Label errorLabel = new Label();
	final ListBox categoryListBox = new ListBox();
	final TextBox usernameField = new TextBox();
	final PasswordTextBox passwordField = new PasswordTextBox();	
	final HorizontalPanel horizontalTitle = new HorizontalPanel();
	final HorizontalPanel horizontalAuthor = new HorizontalPanel();
	final HorizontalPanel horizontalPublisher = new HorizontalPanel();
	final HorizontalPanel horizontalCategory = new HorizontalPanel();
	final HorizontalPanel horizontalUsername = new HorizontalPanel();
	final HorizontalPanel horizontalPassword = new HorizontalPanel();
	final Button sendButton = new Button("Dodaj ksiazke");
	final Button loginButton = new Button("Zaloguj");
	final RootPanel rp = RootPanel.get();
	final VerticalPanel dialogLoginPanel = new VerticalPanel();
	final VerticalPanel dialogBookPanel = new VerticalPanel();
	final TabLayoutPanel tlp = new TabLayoutPanel(2, Unit.EM);
	
	private final UserServicesAsync userServices = GWT.create(UserServices.class);

	public void onModuleLoad() {
		dialogLoginPanel.setStyleName("login-table");
		createAddBookForm(dialogBookPanel, titleField, authorField,
				publisherField, categoryListBox, horizontalTitle,
				horizontalAuthor, horizontalPublisher, horizontalCategory,
				sendButton);
		createLoginForm(dialogLoginPanel, usernameField, passwordField, horizontalUsername, horizontalPassword, loginButton);
		
	    tlp.add(dialogBookPanel, "Dodaj ksiazke");
	    tlp.add(new HTML("Szukaj ksiazke"), "Szukaj ksiazke");

	    rp.add(dialogLoginPanel);		

		loginButton.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				
				userServices.login(usernameField.getText(), passwordField.getText(),
						new AsyncCallback<String>() {
							public void onFailure(Throwable caught) {
							}

							public void onSuccess(String result) {
								if ("0".equals(result)) {
									Window.alert("Zla nazwa uzytkownika lub haslo");
								} else {
									rp.clear();
									rp.add(tlp);
								}
							}
						});			
				}
		});
	}

	private void createLoginForm(VerticalPanel dialogLoginPanel,
			TextBox usernameField, TextBox passwordField, HorizontalPanel horizontalUsername, HorizontalPanel horizontalPassword, Button loginButton) {

		final Label usernameLabel = new Label();
		usernameLabel.setText("Nazwa uzytkownika:");
		horizontalUsername.setStyleName("horizontal-username");
		horizontalUsername.add(usernameLabel);
		horizontalUsername.add(usernameField);
		
		final Label passwordLabel = new Label();
		passwordLabel.setText("Haslo:");
		horizontalPassword.setStyleName("horizontal-password");
		horizontalPassword.add(passwordLabel);
		horizontalPassword.add(passwordField);
		
		dialogLoginPanel.add(horizontalUsername);		
		dialogLoginPanel.add(horizontalPassword);		
		dialogLoginPanel.add(loginButton);

	}

	private void createAddBookForm(VerticalPanel dialogBookPanel, TextBox titleField, TextBox authorField, TextBox publisherField,
			final ListBox categoryListBox, HorizontalPanel horizontalTitle,
			HorizontalPanel horizontalAuthor,
			HorizontalPanel horizontalPublisher,
			HorizontalPanel horizontalCategory, Button sendButton) {

		sendButton.addStyleName("sendButton");				
		categoryListBox.addItem("Thriller");
		categoryListBox.addItem("Historia");
		categoryListBox.addItem("Dramat");
		categoryListBox.addItem("Przewodnik");
		
		final Label titleLabel = new Label();
		titleLabel.setText("Tytul:");
		horizontalTitle.add(titleLabel);
		horizontalTitle.add(titleField);
		horizontalTitle.setStyleName("horizontal-title");

		final Label authorLabel = new Label();
		authorLabel.setText("Autor:");
		horizontalAuthor.add(authorLabel);
		horizontalAuthor.add(authorField);
		horizontalAuthor.setStyleName("horizontal-author");

		final Label publisherLabel = new Label();
		publisherLabel.setText("Wydawca:");
		horizontalPublisher.add(publisherLabel);
		horizontalPublisher.add(publisherField);
		horizontalPublisher.setStyleName("horizontal-publisher");

		
		final Label categoryLabel = new Label();
		categoryLabel.setText("Gatunek:");
		horizontalCategory.add(categoryLabel);		
		horizontalCategory.add(categoryListBox);
		horizontalCategory.setStyleName("horizontal-category");
		
		sendButton.setStyleName("addBookButton");
		
		dialogBookPanel.add(horizontalAuthor);
		dialogBookPanel.add(horizontalTitle);
		dialogBookPanel.add(horizontalPublisher);
		dialogBookPanel.add(horizontalCategory);
		dialogBookPanel.add(sendButton);
	}
}
